package com.wenwen.social.application.impl;

import com.wenwen.social.application.FollowLineApplication;
import com.wenwen.social.application.dto.*;
import com.wenwen.social.common.exception.ServiceException;
import com.wenwen.social.constant.errorcode.CommonErrorConstant;
import com.wenwen.social.domain.model.*;
import com.wenwen.social.domain.service.FollowLineService;
import com.wenwen.social.domain.service.MessageService;
import com.wenwen.social.domain.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

@Service
public class FollowLineApplicationImpl implements FollowLineApplication {
	@Autowired
	private FollowLineService followLineService;

	@Autowired
	private MessageService messageService;

	@Autowired
	private UserService userService;

	@Override
	public PageResult<MessageDTO> queryFollowLineMsg(PageRequest pageRequest, Long uid) {

		Integer pageSize = pageRequest.getPageSize();
		Integer pageNum = pageRequest.getPageNum();

		List<FollowLine> personLines = followLineService.query(uid, pageNum, pageSize);

		if(personLines == null || personLines.isEmpty())
			return new PageResult<>(new ArrayList<>(),pageNum,pageSize,0);

		List<Long> msgIds = personLines.stream().map(FollowLine::getMessageId).collect(toList());
		if(msgIds == null || msgIds.isEmpty()){
			return new PageResult<>(new ArrayList<>(),pageNum,pageSize,0);
		}

		Long count = followLineService.count(uid);

		List<Message> messages = messageService.queryBatch(msgIds);

		Set<Long> userIdset = messages.stream().map(message -> message.getUid()).collect(Collectors.toSet());

		if (userIdset == null || userIdset.isEmpty()) {
			throw new ServiceException(CommonErrorConstant.USER_NOT_EXIST_CODE, CommonErrorConstant.USER_NOT_EXIST_MSG);
		}

		Map<Long, User> userMap = userService.getUsersMap(new ArrayList<Long>(userIdset));

		return this.buildqueryPersonLineMsgResult(messages, userMap, count, pageRequest);
	}

	private PageResult<MessageDTO> buildqueryPersonLineMsgResult(List<Message> messageList, Map<Long, User> userMap, Long totalRows, PageRequest pageRequest) {
		List<MessageDTO> resultList = new ArrayList<>();

		for (Message message : messageList) {

			MessageDTO messageDTO = new MessageDTO();

			BeanUtils.copyProperties(message, messageDTO);

			User user = userMap.get(message.getUid());

			if (user != null) {
				UserDTO userDTO = new UserDTO();

				BeanUtils.copyProperties(user, userDTO);

				messageDTO.setUser(userDTO);
			}

			Medias medias = message.getMedias();

			if (medias != null) {
				MediasDTO mediasDTO = new MediasDTO();
				List<MdataDTO> mdataDTOList = new ArrayList<>();

				if (medias.getMdatas()!=null){
					for (Mdata mdata : medias.getMdatas()) {
						MdataDTO mdataDTO = new MdataDTO();
						BeanUtils.copyProperties(mdata, mdataDTO);
						mdataDTOList.add(mdataDTO);
					}
				}
				mediasDTO.setMdatas(mdataDTOList);
				mediasDTO.setVersion(medias.getVersion());
				messageDTO.setMedias(mediasDTO);
			}

			resultList.add(messageDTO);
		}

		PageResult<MessageDTO> pageResult = new PageResult<>(resultList, pageRequest.getPageNum(), pageRequest.getPageSize(), totalRows.intValue());

		return pageResult;
	}
}
